package com.primeton.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;

public class SocketClient {

	private static final String host = "localhost";

	private static final int port = 60001;

	private Socket socket;

	public SocketClient() {
		try {
			socket = new Socket(host, port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("create socket client error code : 1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println("io error code : 2");
		}
	}

	public void sendObj(Fick fick) {
		try {
			OutputStream os = socket.getOutputStream();
			Output output = new Output(os);
			Kryo kryo = new Kryo();
			kryo.register(Fick.class);
			kryo.writeObject(output, fick);
			os.write(output.toBytes());
			System.out.println(os.toString());
			os.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new SocketServer().start();
		SocketClient client = new SocketClient();
		Fick fick = new Fick("songfei", 25);
		client.sendObj(fick);
	}

}
